//
//  LBSuperMarketDetailsBottomView.m
//  loveBeen
//
//  Created by 韩天助 on 11/20/16.
//  Copyright © 2016 loveBeen. All rights reserved.
//

#import "LBSuperMarketDetailsBottomView.h"
//#import "NSAttributedString+CZAdditon.h"
#import "LBMinusPlusView.h"

@interface LBSuperMarketDetailsBottomView()<LBMinusPlusViewDelegate>

// 减号按钮
@property (weak, nonatomic) UIButton *minusBtn;
//小红点
@property(weak,nonatomic) UIButton *redBtn;

@end

@implementation LBSuperMarketDetailsBottomView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype)init{
    if (self = [super init]) {
        [self setUpUI];
    }
    return self;
}

- (void)setModel:(LBSuperMarketProducts *)model{
    _model = model;
    
    _model.isAnimation = NO;
    
    _plusMinusView.model = _model;
}

- (void)buyCarClick:(UIButton *)sender {
    
    
    if ([_delegate respondsToSelector:@selector(superMarketDetailsBottomView:)]) {
        [_delegate superMarketDetailsBottomView:self];
    }
}

//加减号代理方法实现小红点
- (void)minusPlusView:(LBMinusPlusView *)minusPlusView{
    NSString *allCount = [LBPlusMinusTool sharedPlusMinusTool].allCount;
    if (allCount.intValue == 0) {
        self.redBtn.hidden = YES;
    }else{
        self.redBtn.hidden = NO;
        [self.redBtn setTitle:allCount forState:UIControlStateNormal];
    }
}

- (void)setUpUI{
    //    self.backgroundColor = [UIColor cz_colorWithHex:0xf4f4f4];
    
    UIButton *btn = [[UIButton alloc]init];
    [self addSubview:btn];
    [btn addTarget:self action:@selector(clickCollectionBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setAttributedTitle:[NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:@"non_collection"] imageWH:20 title:@"收藏" fontSize:8 titleColor:[UIColor blackColor] spacing:2] forState:UIControlStateNormal];
    
    [btn setAttributedTitle:[NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:@"already_collect"] imageWH:20 title:@"收藏" fontSize:8 titleColor:[UIColor blackColor] spacing:2] forState:UIControlStateSelected];
    
    //    MARK: 设置按钮的大小
    [btn sizeToFit];
    //    MARK: 设置按钮的自动换行
    btn.titleLabel.numberOfLines = 0;
    //    MARK: 设置按钮的文本对齐方式
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    
    UILabel *addLabel = [[UILabel alloc]init];
    addLabel.text = @"添加商品:";
    addLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:addLabel];
    
    LBMinusPlusView *minusPlusView = [[[UINib nibWithNibName:@"LBMinusPlusView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
    [self addSubview:minusPlusView];
    minusPlusView.backgroundColor = [UIColor cz_colorWithHex:0xf4f4f4];
    minusPlusView.delegate = self;
    
    UIButton *buyCarBtn = [[UIButton alloc]init];
    [buyCarBtn setImage:[UIImage imageNamed:@"shopCart"] forState:UIControlStateNormal];
    [self addSubview:buyCarBtn];
    buyCarBtn.layer.cornerRadius = 22;
    buyCarBtn.layer.masksToBounds = YES;
    buyCarBtn.backgroundColor = [UIColor cz_colorWithHex:0xfecf0b];
    [buyCarBtn addTarget:self action:@selector(buyCarClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *redBtn = [[UIButton alloc]init];
    //    [redBtn setImage:[UIImage imageNamed:@"v2_notice_dot"] forState:UIControlStateNormal];
    [redBtn setBackgroundImage:[UIImage imageNamed:@"v2_notice_dot"] forState:UIControlStateNormal];
    [self addSubview:redBtn];
    redBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    redBtn.userInteractionEnabled = NO;
    
    NSString *allCount = [LBPlusMinusTool sharedPlusMinusTool].allCount;
    if (allCount.intValue == 0) {
        redBtn.hidden = YES;
    }else{
        redBtn.hidden = NO;
        [redBtn setTitle:allCount forState:UIControlStateNormal];
    }
    
    // 关联
    self.plusMinusView = minusPlusView;
    self.minusBtn = minusPlusView.minusBtn;
    self.redBtn = redBtn;
    
    //MARK: 约束
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(8);
        make.centerY.offset(0);
    }];
    
    [addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn.mas_right).offset(10);
        make.centerY.offset(0);
    }];
    
    [minusPlusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addLabel.mas_right).offset(2);
        make.centerY.offset(0);
        make.size.mas_equalTo(CGSizeMake(84, 30));
    }];
    
    [buyCarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-4);
        make.top.offset(-10);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    [redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(buyCarBtn);
        make.top.equalTo(buyCarBtn);
    }];
}

- (void)clickCollectionBtn:(UIButton *)sender{
    if (sender.selected) {
        NSLog(@"收藏成功");
    }else{
        NSLog(@"取消收藏");
    }
    sender.selected = !sender.selected;
}

@end
